<?php

set_time_limit(0);

$mysql_server_name="rm-2zeq62pf3310quj6d.mysql.rds.aliyuncs.com"; //数据库服务器名称
$mysql_username="fbwwebtest"; // 连接数据库用户名
$mysql_password="Fbwtest123"; // 连接数据库密码
$mysql_database="fbwdatabasetest"; // 数据库的名字

$mysqli = new mysqli($mysql_server_name, $mysql_username, $mysql_password, $mysql_database);

date_default_timezone_set("Asia/Shanghai");



if(date('G')=='10')
{
	$sql = "SELECT `shopid`,`balance` FROM `91fbw_shop_salesman` WHERE type='partner' and status='1' and balance>0";
	$result = $mysqli->query($sql);
	while ($row = mysqli_fetch_assoc($result))
	{
		$mysqli->query("insert into 91fbw_shop_salesman_withdrawal (shopid,accountamount,accountmonth,status,created_at,updated_at)
        values ('".$row['shopid']."','".$row['balance']."','".date('Y-m-01',time() - 36000)."','0','".date('Y-m-d H:i:s',time()).
            "','".date('Y-m-d H:i:s',time())."')");
	}
	$sql = "SELECT `shopid`,`balance` FROM `91fbw_shop_salesman` WHERE type='common' and status='1' and balance>0";
	$result = $mysqli->query($sql);
	while ($row = mysqli_fetch_assoc($result))
	{
		$mysqli->query("insert into 91fbw_shop_salesman_withdrawal (shopid,accountamount,accountmonth,status,created_at,updated_at)
          values ('".$row['shopid']."','".$row['balance']."','".date('Y-m-01',time() - 36000)."','0','".date('Y-m-d H:i:s',time()).
            "','".date('Y-m-d H:i:s',time())."')");
	}
	$sql = "update 91fbw_shops set todayevent='0',nextevent=''";
	$result = $mysqli->query($sql);
	$sql = "select a.*,b.secondclassid from 91fbw_shop_events a,91fbw_shops b where a.shopid=b.id and b.status='1' and  a.event='".date('Y-m-d',time())."'";
	$result = $mysqli->query($sql);
	$data = $result->fetch_assoc($result);
	while ($row = mysqli_fetch_assoc($result))
	{
		$mysqli->query("update 91fbw_shops set todayevent='1',lastevent='" . date('Y-m-d', time()) . "' where id=" . $row['shopid']);
		$mysqli->query("insert into 91fbw_shop_historyevents (shopid,event) values ('" . $row['shopid'] . "','" . date('Y-m-d', time()) . "')");
		$class = ['7','8','10','11','12','13','15','16','19','23','24','25','26'];
		if(in_array($row['secondclassid'],$class))
		{
			$ordercount = $mysqli->query("select count(*) as ordercount from 91fbw_orders where orderamount >= 30 and shopid = '" . $row['shopid'] . "'");
			while ($ordercountrow = mysqli_fetch_assoc($ordercount))
			{
				if($ordercountrow['ordercount'] >= 3)
				{
					$mysqli->query("update 91fbw_shop_contract set eventdays=case when eventdays = 0 then 2 else eventdays+1 end where shopid='" . $row['shopid'] . "' and starttime<='" . date('Y-m-d', time()) . "' and lasttime>='" . date('Y-m-d', time()) . "'");
				}
			}
		}
		else
		{
			$ordercount = $mysqli->query("select count(*) as ordercount from 91fbw_orders where orderamount >= 50 and shopid = '" . $row['shopid'] . "'");
			while ($ordercountrow = mysqli_fetch_assoc($ordercount))
			{
				if($ordercountrow['ordercount'] >= 3)
				{
					$mysqli->query("update 91fbw_shop_contract set eventdays=case when eventdays = 0 then 2 else eventdays+1 end where shopid='" . $row['shopid'] . "' and starttime<='" . date('Y-m-d', time()) . "' and lasttime>='" . date('Y-m-d', time()) . "'");
				}
			}
		}
	}
	$sql = "select * from (select * from 91fbw_shop_events where event>'".date('Y-m-d',time())."' order by event asc ) t group by shopid";
	$result = $mysqli->query($sql);
	$data = $result->fetch_assoc($result);
	while ($row = mysqli_fetch_assoc($result))
	{
		$mysqli->query("update 91fbw_shops set nextevent='".$row['event']."' where id=".$row['shopid']);
	}
	$sql = "delete from 91fbw_shop_events where event<'".date('Y-m-d',time())."'";
	$result = $mysqli->query($sql);
	$sql = "insert into fbwdatabasetest.91fbw_orders select * from fbwdatabase.91fbw_orders where created_at > '".date('Y-m-d', time()-86400)." 00:00:00' and created_at <= '".date('Y-m-d', time()-86400)." 23:59:59'";
	$result = $mysqli->query($sql);
}
if(date('G')=='3')
{
	$sql = "update 91fbw_shops set nightevent='0'";
	$result = $mysqli->query($sql);
}
